package com.software.loginregister.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.software.loginregister.R;
import com.software.loginregister.entity.NotificationItem;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
public class NotificationAdapter extends RecyclerView.Adapter<NotificationAdapter.ViewHolder> {
    private List<NotificationItem> notifications = new ArrayList<>();
    private OnNotificationClickListener listener;
    private OnItemLongClickListener longClickListener;

    // 添加长按监听器接口
    public interface OnItemLongClickListener {
        void onItemLongClick(NotificationItem item);
    }
    public void setOnItemLongClickListener(OnItemLongClickListener listener) {
        this.longClickListener = listener;
    }
    // 修改接口名称和方法
    public interface OnNotificationClickListener {
        void onNotificationClick(NotificationItem item);
    }
    public void setNotifications(List<NotificationItem> notifications) {
        Log.d("NotificationAdapter", "开始设置新数据");
        if (notifications == null) {
            Log.d("NotificationAdapter", "传入的数据为 null");
            this.notifications.clear();
        } else {
            Log.d("NotificationAdapter", "传入的数据大小: " + notifications.size());
            this.notifications.clear();
            this.notifications.addAll(notifications);
        }
        Log.d("NotificationAdapter", "设置后的数据大小: " + this.notifications.size());
        notifyDataSetChanged();
    }
    @Override
    public int getItemCount() {
        int size = notifications.size();
        Log.d("NotificationAdapter", "getItemCount 被调用，当前数据大小: " + size);
        return size;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_notification, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        NotificationItem item = notifications.get(position);
        Log.d("NotificationAdapter", "绑定位置 " + position + " 的数据");
        holder.tvTitle.setText(item.getTitle());
        holder.tvContent.setText(item.getContent());

        // 格式化时间显示
        try {
            SimpleDateFormat inputFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.getDefault());
            SimpleDateFormat outputFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
            Date date = inputFormat.parse(item.getCreatedAt());
            holder.tvTime.setText(outputFormat.format(date));
        } catch (Exception e) {
            holder.tvTime.setText(item.getCreatedAt());
        }

        // 设置已读/未读状态
        holder.itemView.setAlpha(item.isRead() ? 0.6f : 1.0f);

        // 添加空值检查
        if (holder.ivFood != null) {  // 确保 ImageView 不为 null
            if (item.getFood() != null && item.getFood().getFoodImageUrl() != null) {
                holder.ivFood.setVisibility(View.VISIBLE);
                Glide.with(holder.itemView.getContext())
                        .load(item.getFood().getFoodImageUrl())
                        .into(holder.ivFood);
            } else {
                holder.ivFood.setVisibility(View.GONE);
            }
        }

        // 设置点击事件
        holder.itemView.setOnClickListener(v -> {
            if (listener != null) {
                listener.onNotificationClick(item);
            }
        });
        // 设置长按监听器
        holder.itemView.setOnLongClickListener(v -> {
            if (longClickListener != null) {
                longClickListener.onItemLongClick(item);
                return true;
            }
            return false;
        });
    }
    // 添加删除通知的方法
    public void removeNotification(Long notificationId) {
        for (int i = 0; i < notifications.size(); i++) {
            if (notifications.get(i).getId().equals(notificationId)) {
                notifications.remove(i);
                notifyItemRemoved(i);
                break;
            }
        }
    }
    // 清空所有通知
    public void clearAll() {
        int size = notifications.size();
        notifications.clear();
        notifyItemRangeRemoved(0, size);
    }

    // 添加获取通知列表的方法
    public List<NotificationItem> getNotifications() {
        return notifications;
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvTitle;
        TextView tvContent;
        TextView tvTime;
        ImageView ivFood;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            tvTitle = itemView.findViewById(R.id.tv_notification_title);
            tvContent = itemView.findViewById(R.id.tv_notification_content);
            tvTime = itemView.findViewById(R.id.tv_notification_time);
            ivFood = itemView.findViewById(R.id.iv_notification_food);
        }
    }
}